package com.zglc.ms.shop.dao;

import com.google.common.collect.Lists;
import com.zglc.ms.base.util.DateUtil;
import com.zglc.ms.shop.model.UserPoint;
import com.zglc.ms.shop.model.UserPointRecord;
import com.zglc.ms.shop.util.PointTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.List;

@Repository
public class PointUserDao {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public int getPointUserByUserId(int userId, int pointType, long totalPoint) {
        final String sql = "SELECT count(1) cnt FROM point_user  WHERE user_id = ? And type = ? And available_point >= ?";
        logger.debug("pointsql------" + sql);
        logger.debug("userId" + userId + " ---pointType" + pointType + "----totalPoint" + totalPoint);
        try {
            Integer count = jdbcTemplate.query(sql.toString(), new Object[] { userId, pointType, totalPoint }, (rs) -> {
                Integer i = null;
                while (rs.next()) {
                    i = rs.getInt("cnt");
                }
                return i;
            });
            return count;
        } catch (DataAccessException e) {
            logger.info(e.getLocalizedMessage());
            return -1;
        }
    }

    public boolean updateUserPoint(int userId, int point, int type) {
        final String sql = " UPDATE point_user SET available_point = available_point - ?, updated_at = NOW() WHERE user_id = ? AND type = ? AND available_point >= ?";
        try {
            return jdbcTemplate.update(sql, new Object[] { point, userId, type, point }) == 1;
        } catch (DataAccessException e) {
            logger.info(e.getLocalizedMessage());
            return false;
        }
    }

    public List<UserPoint> getUserPoint(int userId) {
        final String sql = "SELECT type, user_id, available_point, total_point FROM point_user WHERE user_id = ? ";
        try {
            return jdbcTemplate.query(sql, new Object[] { userId }, (rs) -> {
                List<UserPoint> list = Lists.newArrayList();
                UserPoint userPoint = null;
                while (rs.next()) {
                    userPoint = new UserPoint();
                    userPoint.setAvailablePoint(rs.getInt("available_point"));
                    userPoint.setTotalPoint(rs.getInt("total_point"));
                    userPoint.setUserId(rs.getInt("user_id"));
                    userPoint.setType(rs.getInt("type"));
                    list.add(userPoint);
                }
                return list;
            });
        } catch (DataAccessException e) {
            logger.info(e.getLocalizedMessage());
        }

        return null;
    }

    public List<UserPointRecord> getUserPointRecord(int userId, int type, int page, int pageNum) {
        final String sql = " SELECT action_type, change_point, remark, tran_time, trans_id, point_type "
                + " FROM point_user_change_detail WHERE user_id = ? AND type = ? " + " ORDER BY id DESC LIMIT "
                + pageNum * (page - 1) + "," + pageNum;
        try {
            return jdbcTemplate.query(sql, new Object[] { userId, type }, (rs) -> {
                List<UserPointRecord> list = Lists.newArrayList();
                UserPointRecord record = null;
                while (rs.next()) {
                    record = new UserPointRecord();
                    record.setType(rs.getInt("point_type"));
                    record.setChangeValue(rs.getInt("change_point"));
                    record.setTransDate(DateUtil.date2String(rs.getTimestamp("tran_time")));
                    String string = rs.getString("remark");
                    record.setTransId(rs.getString("trans_id"));
                    int int1 = rs.getInt("action_type");
                    if (StringUtils.isEmpty(string)) {
                        switch (int1) {
                        case 0:
                            record.setTypeName(PointTypeEnum.INVEST.getName());
                            break;
                        case 1:
                            record.setTypeName(PointTypeEnum.SIGN.getName());
                            break;
                        case 2:
                            record.setTypeName(PointTypeEnum.SIGN_CONTINUOUS.getName());
                            break;
                        case 3:
                            record.setTypeName(PointTypeEnum.OPEN_INVEST_PLAN.getName());
                            break;
                        case 4:
                            record.setTypeName(PointTypeEnum.COMMENT.getName());
                            break;
                        case 5:
                            record.setTypeName(PointTypeEnum.ADVICE.getName());
                            break;
                        case 6:
                            record.setTypeName(PointTypeEnum.COMMENT_PICK.getName());
                            break;
                        case 7:
                            record.setTypeName(PointTypeEnum.ADVICE_PICK.getName());
                            break;
                        case 8:
                            record.setTypeName(PointTypeEnum.ACTIVITY_REWARD.getName());
                            break;
                        case 22:
                            record.setTypeName(PointTypeEnum.LUCKY_REWARD.getName());
                            break;
                        default:
                            break;
                        }
                    } else {
                        record.setTypeName(string);
                    }
                    list.add(record);
                }
                return list;
            });
        } catch (DataAccessException e) {
            logger.info(e.getLocalizedMessage());
        }

        return null;
    }

    public Integer getUserPointRecordCnt(int userId, int type) {
        final String sql = " SELECT count(1) cnt FROM " + "point_user_change_detail"
                + " WHERE user_id = ? AND type = ? ";
        try {
            Integer count = jdbcTemplate.query(sql.toString(), new Object[] { userId, type }, (rs) -> {
                Integer i = null;
                while (rs.next()) {
                    i = rs.getInt("cnt");
                }
                return i;
            });
            return count;
        } catch (DataAccessException e) {
            logger.info(e.getLocalizedMessage());
        }
        return null;
    }

}
